package com.google.code.gestion_de_conferences;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;

import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ReservationsFilterListActivity extends ListActivity 
{
	AABDatabaseManager db;
	
	//this is to know if there is or not rooms to display so we display the context menu 
	Boolean res_exist = true; 
	String[] getReservations;

	/**
	 * This function is launched when the activity is launched
	 * 
	 * @author Rayan Zahab <differentruna@gmail.com>
	 * 
	 */
	public void onCreate(Bundle icicle) 
	{
	    super.onCreate(icicle);
	    
	    Intent bla = getIntent(); 		
        getReservations = bla.getStringArrayExtra("selected");
	    res_exist =  this.displayRervations();
	    
	    //Get the list view
	    ListView list =  getListView();
	   	    
	    //register the context menu for the listview if there is rooms
	    if(res_exist)
	    {
	    	registerForContextMenu(list);
	    }
	}

	public boolean displayRervations()
	{
		   
	    db = new AABDatabaseManager(this);
	  	boolean items_exist = true;
	  	    
	    //array that will hold user data to be displayed
	    String[] values;
	   	
	    //check if we don't have any rooms
		if(getReservations.length==0)
		{
			values = new String[1];
    		values[0] = "No items to display";  
    		items_exist = false;
		}  
		else
		{
			values = new String[getReservations.length];
			String roomName, roomId;
			String userName, userId, date, from, to;
    		String reservationId;
    		for (int position=0; position < getReservations.length; position++)
	    	{
    			reservationId=getReservations[position];
	    	    //{"_id", "date", "from_date", "to_date", "room", "user"};
	    		//display full name - capacity
    			roomId = db.getValueByParam(0,4,"Reservation",reservationId);
    			roomName = db.getValueByParam(0,1,"Room",roomId);
    			userId = db.getValueByParam(0,5,"Reservation",reservationId);
    			userName = db.getValueByParam(0,1,"User",userId);
    			from = db.getValueByParam(0,2,"Reservation",reservationId);
    			to = db.getValueByParam(0,3,"Reservation",reservationId);
    			date = db.getValueByParam(0,1,"Reservation",reservationId);	    		    			    			
	    		
	    		values[position] = roomName+" \n "+ userName+"\n"+date+"\n"+from+" => "+to; 	    			    			
	    	} 
		}
		    	    	
	    // Use your own layout
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.room_row_layout, R.id.label, values);
	    setListAdapter(adapter);
	    
	    return items_exist;
	}
	
	 /**
     * This function allow to create the context menu when clicking on a user
     * 
     * @author Hyame Alameddine <hyame.alameddine@gmail.com>
     * 
     * @param ContextMenu menu
     * @param View v
     * @param ContextMenuInfo menuInfo
     */
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
    {
         super.onCreateContextMenu(menu, v, menuInfo);
         
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.reservation_context_menu, menu);                                       
    }
    
    /**
     * This function allows to set the action for the selected menu item
     * 
     * @author Hyame Alameddine <hyame.alameddine@gmail.com>
     * 
     * @param MenuItem item
     * @return boolean
     */
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
            //Get the position of the selected item
            AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
            Intent i;
            
        switch (item.getItemId())    
        {
                           
                  case R.id.delete:     
                    
                      Reservation reservation = new Reservation();                                          
                      //get the selected user data
                      ArrayList<Object> dbResDel = db.getObjectFromPosition(info.position, reservation);
                            
                      //set the user id to edit
                      reservation.setId(Integer.parseInt(dbResDel.get(0).toString()));
                            
                      //delete user from db
                      db.deleteRow (reservation);
                            
                      //redirect to users list
                      i = new Intent(getApplicationContext(), ReservationsListActivity.class);
                      startActivity(i);
                  
                      Toast.makeText(this, "Reservation successfully deleted", Toast.LENGTH_LONG).show();
              break;                  
        }

      return true;
    }
    

	
	
	/**
	   * This function create the menu in the user info activity
	   * @author Moustafa Chouraiki <mrsafu@gmail.com>
	   * @param menu
	   */
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.menu, menu);              
       return true;
   }
   
   /**
     * This function handles clicking any of the menu's items
     * @author Rayan Zahab <differentruna@gmail.com> 
     * @author Hyame Alameddine <hyame.alameddine@gmail.com>
     * @param menu
     */
   @Override
   public boolean onOptionsItemSelected(MenuItem item) 
    {       
        Intent i;
        
        switch (item.getItemId()) 
        {
         
        case R.id.users:     
              i = new Intent(getApplicationContext(), UsersListActivity.class);
              startActivity(i);
              break;
        case R.id.reservations:     
              i = new Intent(getApplicationContext(), ReservationsListActivity.class);
              startActivity(i);
              break;
        case R.id.conferences:     
              i = new Intent(getApplicationContext(), RoomsListActivity.class);
              startActivity(i);
              break;
        case R.id.edit_profile:                         
              i = new Intent(getApplicationContext(), RegisterActivity.class);
              i.putExtra("editAdmin", 1);
              startActivity(i);
              break;
        case R.id.logout: 
              i = new Intent(getApplicationContext(), LoginActivity.class);
              startActivity(i);
              break;
        case R.id.help:     
              AlertDialog alertDialog = new AlertDialog.Builder(this).create();
              alertDialog.setTitle("Help!");
              alertDialog.setMessage("Gestion de conference is an application helps you manage reservations, add rooms ,add users and reserve rooms to organize conferences !");
              alertDialog.setIcon(R.drawable.icon);
              alertDialog.show();
              break;
        case R.id.developers: 
              Toast.makeText(this, "Rayan,Hyame,Mostafa!", Toast.LENGTH_LONG).show();
              break;
          
      }
      return true;
  }
}